package com.health.controller;

import com.health.pojo.User;
import com.health.service.UserService;
import com.health.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.UUID;

@Api(tags = "登录相关接口")
@ApiResponses({
        @ApiResponse(code = 200, message = "请求正常", response = Result.class)
})
@RestController
public class LoginController {

    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation("检查项分页条件查询")
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User userLogin = userService.findUserByLogin(user);

        if(userLogin != null){
            //登录成功，生成Token令牌，存储到Redis数据库中-30分钟有效期
            String token = "USER_LOGIN:"+ UUID.randomUUID();
            redisTemplate.opsForValue().set(token, userLogin, Duration.ofMinutes(30));

            return new Result(true, "登录成功", token);
        }

        return new Result(false, "账号或密码输入错误");
    }
}
